package com.hnust.controller;

import com.hnust.domain.record.SystemMessage;
import com.hnust.service.AccountService;
import com.hnust.service.TokenService;
import com.hnust.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 *创建人：张汝往
 *创建时间：2020/10/6
 *描述：账号管理模块
 */
@CrossOrigin("*")
@RestController
@RequestMapping("/api/account")
public class AccountController {

    @Autowired
    private AccountService accountService;

    @Autowired
    private TokenService tokenService;

    /**
     * 点击管理员头像，显示系统信息和账号信息
     * @param token 验证信息
     * @param collegeId 学院ID
     * @param adminId 管理员ID
     * @return
     */
    @GetMapping("/msg")
    public Result<SystemMessage> getSystemMessage(

            @RequestParam(value = "collegeId",required = true)String collegeId,
            @RequestParam(value = "token",required = true)String token,
            @RequestParam(value = "adminId",required = true)String adminId
    ){
        //System.out.println(token+'\n'+collegeId+'\n'+adminId);
        if(tokenService.verifyToken(token,"院管理员")){
            try{
                SystemMessage systemMessage=accountService.getSystemMessage(collegeId,adminId);
                return new Result<SystemMessage>(1,"返回系统信息成功",systemMessage);
            }catch (Exception e){
                return new Result<SystemMessage>(0,"返回系统信息失败",null);
            }
        }else{
            return new Result<SystemMessage>(0,"没有操作权限,请登录",null);
        }
    }

    /**
     * 管理员修改密码
     * @param token token
     * @param adminId 管理员ID
     * @param newPassword 新密码
     * @return
     */
    @GetMapping("/password/change")
    public Result<String> changePassword(
            @RequestParam("token")String token,
            @RequestParam("adminId")String adminId,
            @RequestParam("newPassword")String newPassword
    ){
        if(tokenService.verifyToken(token,"院管理员")){
            if(accountService.changePassword(adminId,newPassword)){
                return new Result<String>(1,"操作成功",null);
            }else{
                return new Result<String>(0,"操作失败",null);
            }
        }else{
            return new Result<String>(0,"没有操作权限,请登录",null);
        }
    }
}
